from django.shortcuts import render,HttpResponse,redirect
from .models import *
from django.core.paginator import Paginator
# Create your views here.
def studentadd(request):
    if request.method=='GET':
        return render(request,'add.html')
    if request.method=='POST':
        stu_name=request.POST.get('stu_name')
        sex=request.POST.get('sex')
        classs=request.POST.get('classs')
        specialized=request.POST.get('specialized')
        img_path=request.POST.get('img_path')
        # with open('kk.jpg','wb')as a:
        #     for x in img_path.chunks():
        #         a.write(x)
        student=Student(stu_name=stu_name,sex=sex,classs=classs,
                        specialized=specialized,img_path=img_path)
        student.save()
        return redirect('stuList')
def get_stu(request,id):
    showget=Student.objects.get(id=id)
    return render(request,'show.html',context={'showget':showget})
def stuList(request):
    showall=Student.objects.all()#获取所有
    page=request.GET.get('page')#传递值取值
    pan=Paginator(showall,3)#每页多少行
    nowpage=pan.get_page(page)#当前页
    curr=nowpage.number#第几页
    lastnumber=curr-1#上一页
    nextnumber=curr+1#下一页
    maxpages=pan.num_pages#最大页
    smallpages=1
    if curr==maxpages:
        nextnumber=maxpages
    if curr==smallpages:
        lastnumber=smallpages
    return render(request,'index.html',context={'showall':showall,'nowpage':nowpage,'curr':curr,
                                                'maxpages':maxpages,'lastnumber':lastnumber,'nextnumber':nextnumber,'pan':pan,'page':page})
def dele(request,id):
    s=Student.objects.get(id=id)
    s.delete()
    return redirect('stuList')
